<?php
namespace app\mobile\controller;

use app\mobile\common\Des;
use app\mobile\model\Config;
use app\mobile\model\Feedback;
use app\mobile\model\FeedbackType;
use app\mobile\model\Member;
use app\mobile\model\Shop;
use think\Cache;
use think\Cookie;
use think\Request;
use think\Session;

/**
 * Class ShopSet
 * @package app\mobile\controller    店铺设置
 */
class ShopSet extends BaseController
{
    /*
     * 设置首页
     */
    public function index()
    {
        $hot_line = (new Config())->where('varname','web_hotline')->value('varvalue');
        return $this->fetch('',[
            'hot_line'   =>  $hot_line,
        ]);
    }
    /*
     * 退出登录
     */
    public function quit(Request $request)
    {
        if ($request->isPost()) {
            if (Session::has('member_id')){
                Session::delete('member_id');
            }
            return ['code'=>200,'message'=>'退出成功'];
        }
    }

    /*
     * 帮助与反馈
     */
    public function help_feedback()
    {
        return $this->fetch('');
    }
    /*
     * 意见反馈
     */
    public function feedback(Request $request)
    {
        //反馈类型
        $type = FeedbackType::where('status', 1)->order(['sort' => 'desc', 'update_time' => 'desc'])->select();
        //提交反馈内容
        if ($request->isPost()) {
            $data = $request->post();
            $data['ip'] = $request->ip();
            $data['create_time'] = time();
            $result = (new Feedback())->allowField(true)->save($data);
            return $result ? ['code' => 200, 'message' => '提交成功'] : ['code' => 0, 'message' => '网络繁忙,请重试'];
        }
        return $this->fetch('', [
            'type' => $type,
        ]);
    }
    /*
     * 账户安全
     */
    public function safety()
    {
        return $this->fetch('');
    }
    /*
     * 修改登录密码
     */
    public function edit_pwd(Request $request)
    {
        if ($request->isPost()){
            $data = $request->post();
            if(Cache::has('verification_login')){
                $verification = Cache::get('verification_login');
            }else{
                return ['code'=>0,'message'=>'验证码已失效'];
            }
            if ($data['verification'] == $verification){
                //更新用户密码
                $data['password'] = md5(md5($data['password']));
                $data['confirmPassword'] = md5(md5($data['confirmPassword']));
                $shop = new Member();
                $result = $shop
                    ->isUpdate(true,['username'=>$data['username']])
                    ->allowField(true)
                    ->validate('member.editPwd')
                    ->save($data);
                if ($result){
                    return ['code'=>200,'message'=>'修改成功'];
                }else{
                    return ['code'=>0,'message'=>$shop->getError()];
                }
            }else{
                return ['code'=>0,'message'=>'验证码错误'];
            }
        }
        //查看绑定号码
        $member_id = Session::get('member_id');
        $username = (new Member())->where('id',$member_id)->value('username');
        return $this->fetch('',[
            'username'  =>  $username,
        ]);
    }
    /*
     * 检测绑定号码(注册用)
     */
    public function check_bind_tel(Request $request)
    {
        if ($request->isPost()){
            //检测新绑定手机号的唯一性
            $new_bind = $request->post('tel');
            $id = (new Member())->where('username',$new_bind)->value('id');
            return !$id?['code'=>200]:['code'=>0,'message'=>'输入的手机号已存在,请更换!'];
        }
    }
    /*
     * 绑定新的手机号码
     */
    public function bind_phone(Request $request)
    {
        if ($request->isPost()){
            //检测新绑定手机号的唯一性
            $new_bind = $request->post('tel');
            $id = (new Member())->where('username',$new_bind)->value('id');
            return !$id?['code'=>200]:['code'=>0,'message'=>'输入的手机号已存在,请更换!'];
        }
        //查看绑定号码
        $username = (new Member())->where('id',Session::get('member_id'))->value('username');
        return $this->fetch('',[
            'username'  =>  $username,
        ]);
    }
    /*
     * 设置|修改 绑定手机数据提交处理
     */
    public function bind_act(Request $request)
    {
        if ($request->isPost()){
            $data = $request->param();
            $member = new Member();
            //查看用户是否绑定过手机号
            $bind = $member->where('id',Session::get('member_id'))->value('username');
            //判断验证码是否正确
            if ($bind){  //绑定过手机号才验证旧绑定手机号码验证码
                if(!Cache::has('verification_1')){return ['code'=>0,'message'=>'验证码已失效,请重新获取'];}
            }
            if(!Cache::has('verification_2')){return ['code'=>0,'message'=>'验证码已失效,请重新获取'];}
            //修改绑定手机号
            if ($request->has('yzm_1','post') && $data['yzm_1'] != Cache::get('verification_1')){
                return ['code'=>0,'message'=>'验证码不正确'];
            }else{
                if ($data['yzm_2'] != Cache::get('verification_2')){
                    return ['code'=>0,'message'=>'验证码不正确'];
                }else{
                    $id = $member->where('username',$data['new_phone'])->value('id');
                    if ($id){return ['code'=>0,'message'=>'输入的绑定手机号已存在,请更换!'];};

                    if ($bind){   //更新绑定手机号操作
                        $result = $member
                            ->where(['username'=>$data['username'],'id'=>Session::get('member_id')])
                            ->update(['username'=>$data['new_phone']]);
                    }else{  //设置绑定手机号和新增登录密码操作
                       $result = $member
                           ->where('id',Session::get('member_id'))
                           ->update(['username'=>$data['new_phone'] , 'password' => md5(md5($data['password']))]);
                    }

                    return $result?['code'=>200,'message'=>'绑定成功']:['code'=>0,'message'=>'网络繁忙,请重试'];
                }
            }
        }
    }
    /*
     * 绑定小号手机
     */
    public function bind_small_act(Request $request)
    {
        if ($request->isPost()){
            $data = $request->post();
            if(!Cache::has('small_code')){return ['code'=>0,'message'=>'验证码已失效,请重新获取'];}
            if ($data['yzm_code'] != Cache::get('small_code')){
                return ['code'=>0,'message'=>'验证码不正确'];
            }else{
                $result = (new Member())
                    ->where('id',Session::get('member_id'))
                    ->update(['username'=>$data['username'],'name'=>$data['nickname']]);
                return $result?['code'=>200,'message'=>'绑定成功']:['code'=>0,'message'=>'网络繁忙,请重试'];
            }
        }
    }
    /*
     * 获取验证码
     */
    public function get_code(Request $request)
    {
        if ($request->isPost()){
            $tel = $request->param('tel');
            $code = mt_rand(100000,999999);  //验证码随机数
            //发送验证码
            $content = $code;

            $result = sendSMS($tel, $content);

            if ($result){
                if ($request->param('act') == 'reg'){   //注册验证码
                    Cache::set('reg_code',$code,300);
                }else if ($request->param('act') == 'small'){   //小号绑定
                    Cache::set('small_code',$code,300);
                }else if ($request->param('act') == 1){     //原账号验证码
                    Cache::set('verification_1',$code,300);
                }else if ($request->param('act') == 2){  //新账号验证码||初始设置绑定
                    Cache::set('verification_2',$code,300);
                }else if ($request->param('login') == 'login'){
                    Cache::set('verification_login',$code,300);   //修改登录密码
                }
            }
            return $result?['code'=>200,'message'=>$code]:['code'=>0,'message'=>'网络繁忙,请重试'];
        }
    }
    /*
     * 关于我们
     */
    public function about_us()
    {
        $hot_line = (new Config())->where('varname','web_hotline')->value('varvalue');
        return $this->fetch('',[
            'hot_line'   =>  $hot_line,
        ]);
    }
    /*
     * 服务条款
     */
    public function terms_server()
    {
        return $this->fetch('');
    }
}